//--------图片---------------
var int_no=true;//防止多次点击注册
var check_pwd_type=false;
var check_pwd_type2=false;
var phone;
var check_mail_type=false;
var remv;//短信删除时用的的变量
var checknu_type=false;
var countdown=60;
var int;
var checknum;//验证码接收
var check_http=true;//判断是否正常访问到网络
var url_userPhoto;
var loadImg;
$(".photoup").bind('change',function(){
    var fordate = new FormData();
    var fils = $(".photoup").get(0).files[0];
    fordate.append('pic', fils);
    var result = uploadfile('photoup');
    if (result.code == 0) {
        url_userPhoto=result.data;
        $(".userphoto").attr({'src': result.data});
        $(".strPhoto").val(result.data);
    } else {
        alert("图片上传失败！");
    }

});
//手机号码验证 （1开头，第二位为[345789]之一，长度为11位）
function checknu(){
    phone = $(".phonenum").val();
    if( !( /^1[345789]\d{9}$/.test(phone) ) ){
        var phonenum=document.getElementById("id_phonenum");
        phonenum.innerHTML="您填写的手机号码有误，请检查重填！";
        phonenum.style.color="red";
        phonenum.style.display="block";
       return false;
    }else {
        var formData = new FormData();
        var url= "/r/isexist";
        formData.append("userTel",phone);
        $.ajax({
            type: "POST",
            cache: false,
            url:url,
            data:formData,
            processData: false,
            contentType: false
        }).done(
            function (res) {
                check_http=false;
                if (res.emp==false){
                    checknu_type=false;
                    var phonenum=document.getElementById("id_phonenum");
                    phonenum.innerHTML="该号码已经注册过";
                    phonenum.style.color="red";
                    phonenum.style.display="block";
                }else {
                    var phonenum=document.getElementById("id_phonenum");
                    phonenum.setAttribute("style","display:none");
                    checknu_type=true;
                }
            }
        ).error(function () {
            alert("启用失败");
        });
    }
    return checknu_type;
}

// //------------------------检查邮箱是否已经注册过---------------
function check_mail() {
    var user_mail = $("#user_mail").val();
    if (user_mail!=""){
        if( !( /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(user_mail) ) ){
            var chenckmail=document.getElementById("chenckmail");
            chenckmail.innerHTML="您填写的邮箱有误，请检查重填！";
            chenckmail.style.color="red";
            chenckmail.style.display="block";
            return false;
        }
        var formData = new FormData();
        var url= "/r/isexist";
        formData.append("userMail",user_mail);
        $.ajax({
            type: "POST",
            cache: false,
            url:url,
            data:formData,
            processData: false,
            contentType: false
        }).done(
            function (res) {
                if (res.emp==false){
                    check_mail_type=false;
                    var chenckmail=document.getElementById("chenckmail");
                    chenckmail.innerHTML="邮箱已经注册";
                    chenckmail.style.color="red";
                    chenckmail.style.display="block";
                }else {
                    var chenckmail=document.getElementById("chenckmail");
                    chenckmail.setAttribute("style","display:none");
                    check_mail_type=true;
                }
            }
        ).error(function () {
            alert("启用失败");
        });
    }
        return check_mail_type;
}
// //----------------获取手机验证码-------------------
function getphonecheck(obj) {
    var chechnu_type2=checknu();
    if (check_pwd_type&&check_pwd_type2){
        if (checknu_type&&chechnu_type2){
            //倒计时------------------------------
            if (countdown==59){
                //手机号码验证 （1开头，第二位为[345789]之一，长度为11位）
                phone = $(".phonenum").val();
                if( !( /^1[345789]\d{9}$/.test(phone) ) ){
                    var message=document.getElementById("message");
                    message.innerHTML="您填写的手机号码有误，请检查重填！";
                    message.style.color="red";
                    message.style.display="block";
                    return false;
                }
                var formData=new FormData();
                formData.append("userTel",$(".phonenum").val());
                $.ajax({
                    type: "POST",
                    cache: false,
                    url:"/r/message",
                    data:formData,
                    processData: false,
                    contentType: false
                }).done(
                    function (res) {
                        if (res.emp==true){
                            var message=document.getElementById("message");
                            message.innerHTML="短信已发送请注意查收";
                            message.style.color="red";
                            message.style.display="block";
                            remv=self.setInterval(remv(),1000);//删除短信提示
                            checknum=res.message;
                            int=self.setInterval("remvchecknum()",60000);
                            //隐藏密码提示
                            var checkpwd=document.getElementById("checkpwd");
                            checkpwd.setAttribute("style","display:none");
                        }else {
                            var message=document.getElementById("message");
                            message.innerHTML="短信发送失败";
                            message.style.color="red";
                            message.style.display="block";
                        }
                    }
                ).error(function () {
                    alert("请求失败");
                });
            }
            if (countdown == 0) {
                obj.removeAttribute("disabled");
                obj.value="免费获取验证码";
                countdown = 60;
                return;
            } else {
                obj.setAttribute("disabled", true);
                obj.value="重新发送(" + countdown + ")";
                countdown--;
            }
            setTimeout(function() {
                    getphonecheck(obj) }
                ,1000)
        }else {
            if ($(".phonenum").val()==""){
                var message=document.getElementById("message");
                message.innerHTML="密码不能为空";
                message.style.color="red";
                message.style.display="block";
            }else if( !( /^1[345789]\d{9}$/.test(phone) ) ){
                var message=document.getElementById("message");
                message.innerHTML="您填写的手机号码有误，请检查重填！";
                message.style.color="red";
                message.style.display="block";
                return false;
            }else if (check_http) {
                //--------------------------------------
                window.location.reload(true);
            }else {
                //--------------------------------------
                var message=document.getElementById("message");
                message.innerHTML="号码已经被注册";
                message.style.color="red";
                message.style.display="block";
            }
        }
    } else if ($("#pwd").val()=="" || $("#pwd2").val()==""){
                var message=document.getElementById("message");
                message.innerHTML="密码不能为空";
                message.style.color="red";
                message.style.display="block";
        } else if (!check_pwd_type2) {
                var checkpwd=document.getElementById("checkpwd");
                checkpwd.innerHTML="密码长度不能小于6";
                checkpwd.style.color="red";
                checkpwd.style.display="block";
        }else {
                var checkpwd=document.getElementById("checkpwd");
                checkpwd.innerHTML="两次密码不统一";
                checkpwd.style.color="red";
                checkpwd.style.display="block";
    }
}
//获取手机验证码结束
$(document).ready(function(e){
    //-------下一步------
    $("#check_phone").on("click",function(){
        if(check_pwd_type&&check_pwd_type2){
            if (checknu_type){
                //通过后获取短信
                //协议判断
                var $isChecked = $("#checkme").is(":checked");
                if(!$isChecked){
                    var message=document.getElementById("message");
                    message.innerHTML="请阅读并同意用户注册协议";
                    message.style.color="red";
                    message.style.display="block";
                    return false;
                }
                // 检查手机验证码是否正确
                if($("#phone_check").val() != checknum){
                    if ($("#phone_check").val()==""){
                        var message=document.getElementById("message");
                        message.innerHTML="请填写验证码";
                        message.style.color="red";
                        message.style.display="block";
                    } else {
                        var message=document.getElementById("message");
                        message.innerHTML="手机验证码有误";
                        message.style.color="red";
                        message.style.display="block";
                    }
                    return false;
                }
                if($(".phonenum").val() == "" || $("#pwd").val() == "" || $("#phone_check").val() == "" || $("#pwd2").val() == ""){
                    var message=document.getElementById("message");
                    message.innerHTML="请输入完整信息";
                    message.style.color="red";
                    message.style.display="block";
                    return false;
                }
                var message=document.getElementById("message");
                message.setAttribute("style","display:none");
                $("#checkphone_div").hide();
                $("#signup_div").show();
            }
        }else if ($("#pwd").val()=="" || $("#pwd2").val()==""){
                var checkpwd=document.getElementById("checkpwd");
                checkpwd.innerHTML="密码不能为空";
                checkpwd.style.color="red";
                checkpwd.style.display="block";
            } else if (!check_pwd_type2) {
                var checkpwd=document.getElementById("checkpwd");
                checkpwd.innerHTML="密码长度不能小于6";
                checkpwd.style.color="red";
                checkpwd.style.display="block";
            }else {
                var checkpwd=document.getElementById("checkpwd");
                checkpwd.innerHTML="两次密码不统一";
                checkpwd.style.color="red";
                checkpwd.style.display="block";
        }
    });
//注册相关------------------------------------------
        $("#sigu_up").on("click",function(){
            var formData = new FormData();
            var mail = $("#user_mail").val();
            if (mail==""){
            } else if( !( /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(mail) ) ){
                var chenckmail=document.getElementById("chenckmail");
                chenckmail.innerHTML="您填写的邮箱有误，请检查重填！";
                chenckmail.style.color="red";
                chenckmail.style.display="block";
                return false;
            }else if (!check_mail_type){
                var chenckmail=document.getElementById("chenckmail");
                chenckmail.innerHTML="邮箱已经注册过";
                chenckmail.style.color="red";
                chenckmail.style.display="block";
                return false;
            }
            if($("#user_name").val() == "" || $("#user_age").val() == ""){
                if ($("#user_name").val() == "" ) {
                    var message2=document.getElementById("message2");
                    message2.innerHTML="用户名不得为空";
                    message2.style.color="red";
                    message2.style.display="block";
                }else {
                    var message2=document.getElementById("message2");
                    message2.innerHTML="年龄不能为空";
                    message2.style.color="red";
                    message2.style.display="block";
                }
                return false;
            }
            var sigu_up_form = {
                "phonenum":$(".phonenum").val(),
                "pwd":$("#pwd").val(),
                "phone_check":$("#phone_check").val(),
                "user_name":$("#user_name").val(),
                "user_age":$("#user_age").val(),
                "user_mail":$("#user_mail").val(),
                "user_sex":$("#user_sex input[name='sex']:checked").val(),
                "marriage":$("#marriage input[name='marriage']:checked").val(),
                "userPhoto":loadImg
            };
            console.table(sigu_up_form);
            formData.append("userAddress",$("#user_address").val());
            formData.append("userTel",$(".phonenum").val());
            formData.append("password",$("#pwd").val());
            formData.append("userName",$("#user_name").val());
            formData.append("userAge",$("#user_age").val());
            formData.append("userMail",$("#user_mail").val());
            formData.append("userSex",$("#user_sex input[name='sex']:checked").val());
            formData.append("marriage",$("#marriage input[name='marriage']:checked").val());
            formData.append("userPhoto",url_userPhoto);
            var url="/r/Reg";
            if (int_no){
                int_no=false;
                $.ajax({
                    type: "POST",
                    cache: false,
                    url:url,
                    data:formData,
                    processData: false,
                    contentType: false
                }).done(
                    function (res) {
                        if (res.emp==true){
                            $(".phonenum").val("");
                            $("#pwd").val("");
                            $("#user_name").val("");
                            $("#phone_check").val("");
                            $("#user_age").val("");
                            $("#user_mail").val("");
                            $("#user_address").val("");
                            alert("注册成功");
                            window.location.href="/l/login";
                        }else {
                            alert("注册失败");
                        }
                    }
                ).error(function () {
                    alert("启用失败");
                });
            }
        });
});
//-------------------删除前端验证码-------------------------
function remvchecknum() {
    checknum="";
    window.clearInterval(int);
}
//密码长度校验
function check_pwd2() {
    if ($("#pwd").val()==""){
        var checkpwd2=document.getElementById("checkpwd2");
        checkpwd2.innerHTML="密码不能为空";
        checkpwd2.style.color="red";
        checkpwd2.style.display="block";
        check_pwd_type2=false;
    }else if ($("#pwd").val().length<6){
        var checkpwd2=document.getElementById("checkpwd2");
        checkpwd2.innerHTML="密码长度不能小于6";
        checkpwd2.style.color="red";
        checkpwd2.style.display="block";
        check_pwd_type2=false;
    }else {
        check_pwd_type2=true;
        var checkpwd2=document.getElementById("checkpwd2");
        checkpwd2.setAttribute("style","display:none");
    }
}
//密码校验
function check_pwd() {
    if ($("#pwd").val()!=$("#pwd2").val()) {
        var checkpwd=document.getElementById("checkpwd");
        checkpwd.innerHTML="两次密码不统一";
        checkpwd.style.color="red";
        checkpwd.style.display="block";
        check_pwd_type=false;
    }else {
        if($("#pwd").val()=="" || $("#pwd2").val()==""){
            var checkpwd=document.getElementById("checkpwd");
            checkpwd.innerHTML="密码不能为空";
            checkpwd.style.color="red";
            checkpwd.style.display="block";
            check_pwd_type=false;
        }else {
            check_pwd_type=true;
            var checkpwd=document.getElementById("checkpwd");
            checkpwd.setAttribute("style","display:none");
        }
    }
}
//年龄校验--------------
function check_age() {
    var m=$("#user_age").val();
    if (m==""){
        var check_age=document.getElementById("check_age");
        check_age.innerHTML="年龄不能为空";
        check_age.style.color="red";
        check_age.style.display="block";
    } else if (m>200) {
        var check_age=document.getElementById("check_age");
        check_age.innerHTML="年龄不能大于200";
        check_age.style.color="red";
        check_age.style.display="block";
    }else {
        var check_age=document.getElementById("check_age");
        check_age.setAttribute("style","display:none");
    }
}
//用户名判断--------
function check_name() {
    if ($("#user_name").val()=="") {
        var check_name=document.getElementById("check_name");
        check_name.innerHTML="用户名不能为空";
        check_name.style.color="red";
        check_name.style.display="block";
    }else {
        var check_name=document.getElementById("check_name");
        check_name.setAttribute("style","display:none");
    }
}
//删除短信提示信息
function remv() {
    var message=document.getElementById("message");
    message.setAttribute("style","display:none");
}
//--------密码显示与隐藏-------------
$("#eye").on("click",function(){
    if($("#eye").attr("class") == "am-icon-eye"){
        $("#eye").removeClass().addClass("am-icon-eye-slash");
        $("#pwd").attr("type","text");
    }else{
        $("#eye").removeClass().addClass("am-icon-eye");
        $("#pwd").attr("type","password");
    }
});
$("#eye2").on("click",function(){
    if($("#eye2").attr("class") == "am-icon-eye"){
        $("#eye2").removeClass().addClass("am-icon-eye-slash");
        $("#pwd2").attr("type","text");
    }else{
        $("#eye2").removeClass().addClass("am-icon-eye");
        $("#pwd2").attr("type","password");
    }
});